#!/bin/bash
usage()
{
    echo "USAGE: [-o] [-u] [-v VERSION_NAME]"
    echo "No ARGS means use default build option"
    echo "WHERE: -o = generate ota package       "
    echo "       -u = generate update.img        "
    echo "       -v = set build version name for output image folder"
    exit 1
}
#上面这个函数用于提示输入
BUILD_UPDATE_IMG=true  #默认是这个，从后面看是打包镜像
BUILD_OTA=false
BUILD_VERSION="IMAGES"

# check pass argument
while getopts "ouv:" arg   #操作者键盘输入命令
do
    case $arg in
        o)
            echo "will build ota package"
            BUILD_OTA=true
            ;;
        u)
            echo "will build update.img"
            BUILD_UPDATE_IMG=true                         #居然只有两次？？？？？？？？？？？？？？？？？？？？？？？？？？？？？？？？？？？？？？？？？
            ;;
        v)
            BUILD_VERSION=$OPTARG                         #居然只有两次？？？？？？？？？     $OPTARG只有一次？？？？？？？？？？？？？？？？？？？？？？？？？？？？？？？？
            ;;
        ?)
            usage ;;
    esac
done

source build/envsetup.sh >/dev/null && setpaths  #引用envsetup.sh,setpaths是envsetup.sh里的一个函数，作用是
                                                #sets ANDROID_BUILD_PATHS to what it is adding  #
                                                #   to PATH
TARGET_PRODUCT=`get_build_var TARGET_PRODUCT`  #get_build_var也是一个函数，Get the exact value of a build variable.?????????????????????????????????????????????????????????
                                                  # TARGET_PRODUCT是一个变量
#set jdk version  jdk环境 有三个
export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64
export PATH=$JAVA_HOME/bin:$PATH
export CLASSPATH=.:$JAVA_HOME/lib:$JAVA_HOME/lib/tools.jar
# source environment and chose target product
DEVICE=`get_build_var TARGET_PRODUCT`
BUILD_VARIANT=`get_build_var TARGET_BUILD_VARIANT`
UBOOT_DEFCONFIG=rk3288_secure_defconfig
KERNEL_DEFCONFIG=rockchip_defconfig                 #一共引用了两次，全在注释里
KERNEL_DTS=rk3288-tb
#KERNEL_DTS=rk3288-evb-act8846
PACK_TOOL_DIR=RKTools/linux/Linux_Pack_Firmware     #打包工具文件夹地址
IMAGE_PATH=rockdev/Image-$TARGET_PRODUCT            #镜像地址
export PROJECT_TOP=`gettop`                         #gettop是envsetup.sh中685行的函数，作用是找到顶级目录

#lunch $DEVICE-$BUILD_VARIANT

PLATFORM_VERSION=`get_build_var PLATFORM_VERSION`     #?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
DATE=$(date  +%Y%m%d.%H%M)
STUB_PATH=Image/"$KERNEL_DTS"_"$PLATFORM_VERSION"_"$DATE"_RELEASE_TEST        #KERNEL_DTS就是51行那个变量
STUB_PATH="$(echo $STUB_PATH | tr '[:lower:]' '[:upper:]')"                   #应该是把小写字母换成大写字母
export STUB_PATH=$PROJECT_TOP/$STUB_PATH                                      #把两个变量设为环境变量
export STUB_PATCH_PATH=$STUB_PATH/PATCHES                                     #这两个环境变量也只在注释里面有

rm out/target/product/rk3288/system/build.prop                                #删除build.prop什么时候生成的？
# build uboot
echo "start build uboot"                                                      #开始打包uboot
cd u-boot && make distclean && make $UBOOT_DEFCONFIG && ./mkv7.sh && cd -     #先进u-boot文件夹 执行1433行distclean命令  ，再make 48行那个东西 再执行mkv7.sh 再回到之前的目录
if [ $? -eq 0 ]; then
    echo "Build uboot ok!"
else
    echo "Build uboot failed!"
    exit 1
fi


# build kernel
echo "Start build kernel"
# cd kernel && make ARCH=arm distclean && make ARCH=arm $KERNEL_DEFCONFIG && make ARCH=arm $KERNEL_DTS.img -j12 && cd -
cd kernel && make ARCH=arm $KERNEL_DTS.img -j4 && cd -            #-j4同时用四线程进行编译/?????????????????????????????????????????????????????????????????????????????????????
if [ $? -eq 0 ]; then
    echo "Build kernel ok!"
else
    echo "Build kernel failed!"
    exit 1
fi

# build wifi ko
echo "Start build wifi ko"
lunch rk3288-user                                                 #lunch是选择平台的命令????????????????????????????????????????????????????/
source device/rockchip/common/build_wifi_ko.sh                    #包含build_wifi_ko.sh?????????????????????????????????????????????????????????????????????????????????????????/

# build android
echo "start build android"
make installclean                                                #执行makefile文件里的installclean
make -j4                                                           #-j4同时用四线程进行编译
./ln.sh
if [ $? -eq 0 ]; then
    echo "Build android ok!"
else
    echo "Build android failed!"
    exit 1
fi

# mkimage.sh
echo "make and copy android images"
./mkimage.sh ota                                              #把ota作为参数传入mkimage.sh
if [ $? -eq 0 ]; then
    echo "Make image ok!"
else
    echo "Make image failed!"
    exit 1
fi

if [ "$BUILD_OTA" = true ] ; then                             #生成ota文件
    INTERNAL_OTA_PACKAGE_OBJ_TARGET=obj/PACKAGING/target_files_intermediates/$TARGET_PRODUCT-target_files-*.zip
    INTERNAL_OTA_PACKAGE_TARGET=$TARGET_PRODUCT-ota-*.zip
    echo "generate ota package"
    make otapackage -j4                                       #打包otapackage
    ./mkimage.sh ota                                          #把ota作为参数传入mkimage.sh
    cp $OUT/$INTERNAL_OTA_PACKAGE_TARGET $IMAGE_PATH/        # 将生成的文件放到指定的文件夹里
    cp $OUT/$INTERNAL_OTA_PACKAGE_OBJ_TARGET $IMAGE_PATH/
fi


mkdir -p $PACK_TOOL_DIR/rockdev/Image/                      #新建文件夹
cp -f $IMAGE_PATH/* $PACK_TOOL_DIR/rockdev/Image/           #强制复制镜像

echo "Make update.img"
cd $PACK_TOOL_DIR/rockdev && ./mkupdate.sh                  #进入文件夹然后执行mkupdate.sh
if [ $? -eq 0 ]; then
    echo "Make update image ok!"
else
    echo "Make update image failed!"
    exit 1
fi
cd -                                                       ##再回到之前的目录

mv $PACK_TOOL_DIR/rockdev/update.img $IMAGE_PATH/          #移动生成的镜像
rm $PACK_TOOL_DIR/rockdev/Image -rf                         #删除之前的文件夹

#mkdir -p $STUB_PATH

#Generate patches


# .repo/repo/repo forall  -c '[ "$REPO_REMOTE" = "rk" ] && { REMOTE_DIFF=`git log $REPO_REMOTE/$REPO_RREV..HEAD`; LOCAL_DIFF=`git diff`; [ -n "$REMOTE_DIFF" ] && { mkdir -p $STUB_PATCH_PATH/$REPO_PATH/; git format-patch $REPO_REMOTE/$REPO_RREV..HEAD -o $STUB_PATCH_PATH/$REPO_PATH; git log -30 --graph --format=short $REPO_REMOTE/$REPO_RREV > $STUB_PATCH_PATH/$REPO_PATH/git-rev.log; } || :; [ -n "$LOCAL_DIFF" ] && { mkdir -p $STUB_PATCH_PATH/$REPO_PATH/; git reset HEAD ./; git diff > $STUB_PATCH_PATH/$REPO_PATH/local_diff.patch; } || :; }'

#Copy stubs
#cp manifest.xml $STUB_PATH/manifest_${DATE}.xml

#mkdir -p $STUB_PATCH_PATH/kernel
#cp kernel/.config $STUB_PATCH_PATH/kernel
#cp kernel/vmlinux $STUB_PATCH_PATH/kernel

#mkdir -p $STUB_PATH/IMAGES/
#cp $IMAGE_PATH/* $STUB_PATH/IMAGES/
#cp build.sh $STUB_PATH/build.sh
#Save build command info
#echo "UBOOT:  defconfig: $UBOOT_DEFCONFIG" >> $STUB_PATH/build_cmd_info
#echo "KERNEL: defconfig: $KERNEL_DEFCONFIG, dts: $KERNEL_DTS" >> $STUB_PATH/build_cmd_info
#echo "ANDROID:$DEVICE-$BUILD_VARIANT" >> $STUB_PATH/build_cmd_info
